package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BankMapper;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BankConfigMapper;
import com.koron.css2.serviceManage.vo.BankConfigVO;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class BankConfigList  implements ServerInterface{
	
	private static Logger logger = LogManager.getLogger(BankConfigList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {	
			JSONObject pa=JSONObject.fromObject(req.getData());
			String companyNo=pa.getString("companyNo");	
			if(StringUtils.isEmpty(companyNo)) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "水司编号不能为空", null);
			}
			BankConfigMapper mapper = factory.getMapper(BankConfigMapper.class,DBSourceUtils.getDbEnv(companyNo));
			//获取列表
			List<BankConfigVO> list =  mapper.selectBankConfigList(companyNo);
			//获取出盘银行
			BankMapper bankMapper=factory.getMapper(BankMapper.class,DBSourceUtils.getDbEnv(companyNo));
			List<SelectVO> bankList=bankMapper.bankSelect();
			for(BankConfigVO config:list) {
				//获取出盘银行名称
				Optional<SelectVO> optional = bankList.stream().filter(o -> Objects.equals(o.getId(), config.getOutBank())).findFirst();
				config.setOutBank(optional.orElse(new SelectVO()).getName());
				//收费类型转换
				if("1".equals(config.getChargingType())) {
					config.setChargingType("代扣");
				}
				if("2".endsWith(config.getChargingType())) {
					config.setChargingType("托收");
				}
				//状态转换
				if("0".equals(config.getStatus())){
					config.setStatus("停用");
				}
				if("1".equals(config.getStatus())){
					config.setStatus("启用");
				}
			}
			info.setData(list);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}


}
